package com.jhx.common.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.jhx.common.app.CommonModule;

/**
 * @author jack
 */
public class ToastUtil {

    public static Builder getInstance() {
        return new Builder();
    }

    public static class Builder{
        private AlertDialog mDialog;
        private Context mPrevContext;
        private Toast mToast;
        private Handler mHandler = new Handler();
        private Runnable r = new Runnable() {

            @Override
            public void run() {
                mToast.cancel();
            }
        };

        public void showDialog(Context context, String text) {
            if (null == context || TextUtils.isEmpty(text)) {
                return;
            }
            if (context instanceof Activity) {
                Activity activityBase = (Activity) context;
                if (!activityBase.isFinishing()) {
                    if (mPrevContext != context) {
                        mDialog = null;
                    }
                    mPrevContext = context;
                    if (null == mDialog) {
                        mDialog = new AlertDialog.Builder(context).setCancelable(true).create();
                    }
                    mDialog.setMessage(text);
                    if (!mDialog.isShowing()) {
                        mDialog.show();
                    }
                }
            }
        }

        public void showToast(Context context, int text) {
            String message = null;
            if (context != null) {
                message = context.getString(text);
            }
            showToast(context, message, Toast.LENGTH_LONG);
        }

        public void showToast(String text) {
            showToast(CommonModule.Companion.getApplication(), text);
        }

        public void showToast(Context context, String text) {
            showToast(context, text, Toast.LENGTH_LONG);
        }

        public void showToast(Context context, int text, int duration) {
            String message = null;
            if (context != null) {
                message = context.getString(text);
            }
            showToast(context, message, duration);
        }

        public void showToast(Context context, String text, int duration) {
            if (null == context || TextUtils.isEmpty(text)) {
                return;
            }
            mHandler.removeCallbacks(r);
            if (mToast != null) {
                mToast.setText(text);
            } else {
                mToast = Toast.makeText(CommonModule.Companion.getApplication(), text, Toast.LENGTH_SHORT);
            }
            mToast.setGravity(Gravity.CENTER, 0, 0);
            int tempDuration = 3000;
            if (duration == Toast.LENGTH_SHORT) {
                tempDuration = 2000;
            } else if (duration == Toast.LENGTH_LONG) {
                tempDuration = 3500;
            }
            mHandler.postDelayed(r, tempDuration);
            mToast.show();
        }
    }
}
